package com.qyhl.webtv.module_user.setting.cancel;

import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 账号注销
 */
@Route(path = ARouterPathConstant.USER_CANCEL)
public class UserCancelActivity extends BaseActivity implements UserCancelContract.UserCancelView {
    @BindView(R2.id.phone)
    EditText phone;
    @BindView(R2.id.code_btn)
    Button codeBtn;
    @BindView(R2.id.code)
    EditText code;
    @BindView(R2.id.commit_btn)
    TextView commitBtn;

    private CountDownTimer mTimer;

    private UserCancelPresenter mPresenter;

    @Override
    protected int getContentView() {
        return R.layout.login_activity_user_cancel;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new UserCancelPresenter(this);

        String username = CommonUtils.getInstance().getUserName();
        phone.setText(username);
        phone.setSelection(phone.getText().length());
        phone.setEnabled(false);
        //初始化计时器
        mTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                setCodeBtn(millisUntilFinished / 1000 + "s后重新获取");
            }

            @Override
            public void onFinish() {
                resetCodeBtn();
            }
        };
    }

    @Override
    protected void setListener() {
        code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtils.isEmpty(s.toString())) {
                    commitBtn.setEnabled(false);
                } else {
                    commitBtn.setEnabled(true);
                }
            }
        });
    }

    //设置获取验证码按钮
    private void setCodeBtn(String time) {
        codeBtn.setText(time);
        codeBtn.setEnabled(false);

    }

    //重新设置验证码按钮
    private void resetCodeBtn() {
        if (mTimer != null) {
            mTimer.cancel();
        }
        codeBtn.setText("获取验证码");
        codeBtn.setEnabled(true);

    }


    @OnClick({R2.id.cancel_btn, R2.id.code_btn, R2.id.commit_btn})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.cancel_btn) {
            finish();
        } else if (id == R.id.code_btn) {
            if (!StringUtils.isMobileNO(phone.getText().toString())) {
                showToast("请填写正确的手机号码！");
                return;
            }
            mTimer.start();
            mPresenter.getCode(phone.getText().toString());
        } else if (id == R.id.commit_btn) {
            if (!StringUtils.isMobileNO(phone.getText().toString())) {
                showToast("请填写正确的手机号码！");
                return;
            }

            if (StringUtils.isEmpty(code.getText().toString())) {
                showToast("请填写验证码！");
                return;
            }
            showLoading();
            mPresenter.cancelAccount(phone.getText().toString(), code.getText().toString());
        }
    }

    @Override
    public void codeSuccess() {
        showToast("验证码已发送，请耐心等待...");
    }

    @Override
    public void codeError(String msg) {
        showToast(msg);
        resetCodeBtn();
    }

    @Override
    public void cancelSuccess() {
        dismissLoading();
        showToast("注销成功！");
        BusFactory.getBus().post(new Event.LoginMessage(false));
        finish();
    }

    @Override
    public void cancelError(String msg) {
        dismissLoading();
        showToast(msg);
    }

    @Override
    protected void onDestroy() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        super.onDestroy();
    }
}
